unit FDBCrtProgress;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, ExtCtrls, CrtTbls, ComCtrls;

type
  TFormDBCrtProgress = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Label2: TLabel;
    PTabla: TPanel;
    LOper: TLabel;
    PBCopia: TProgressBar;
    PBTablas: TProgressBar;
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    FEstado : TTableDefState;
    FDatabaseDef : TDatabaseDef;
    procedure SetTabla( const t: string );
    procedure SetEstado( e: TTableDefState );
    procedure SetTablas( ts: integer );
    procedure SetCopiado(const Value: integer);
    procedure SetACopiar(const Value: integer);
    procedure DBDefProgress(Sender: TDatabaseDef;
                 Table: TTableDef; Index: Integer; State: TTableDefState);
    procedure SetDatabaseDef(const Value: TDatabaseDef);
    property Tabla: string write SetTabla;
    property Estado: TTableDefState write SetEstado;
    property Tablas: integer write SetTablas;
    property ACopiar: integer write SetACopiar;
    property Copiado: integer write SetCopiado;
  public
    { Public declarations }
    property DatabaseDef: TDatabaseDef read FDatabaseDef write SetDatabaseDef;
  end;

implementation

{$R *.DFM}

resourcestring
  rsChequeando  = 'Chequeando ...';
  rsBackupeando = 'Haciendo Backup ...';
  rsCreando     = 'Creando ...';
  rsCopiando    = 'Copiando ...';
  rsError       = 'ERROR ...';

procedure TFormDBCrtProgress.SetTabla( const t: string );
begin
  if PTabla.Caption=t then exit;
  PTabla.Caption := t;
  PBTablas.Position := PBTablas.Position+1;
  PTabla.Update;
end;

procedure TFormDBCrtProgress.SetEstado( e: TTableDefState );
begin
  if e=FEstado then exit;
  FEstado := e;
  case e of
    tdsChecking:  LOper.Caption := rsChequeando;
    tdsBackingup: LOper.Caption := rsBackupeando;
    tdsCreating:  LOper.Caption := rsCreando;
    tdsCopying:
      begin
        LOper.Caption := rsCopiando;
        PBCopia.Position := 0;
        PBCopia.Visible := true;
        PBCopia.Update;
      end;
    tdsError:     LOper.Caption := rsERROR;
    tdsDone:
      begin
        LOper.Caption := '';
        PBCopia.Visible := false;
        PBCopia.Update;
      end;
  end;
  LOper.Update;
end;

procedure TFormDBCrtProgress.SetTablas( ts: integer );
begin
  PBTablas.Position := 0;
  PBTablas.Max := ts;
  Tabla := '';
end;

procedure TFormDBCrtProgress.SetCopiado(const Value: integer);
begin
  if FEstado<>tdsCopying then exit;
  PBCopia.Position := Value;
  PBCopia.Update;
end;

procedure TFormDBCrtProgress.SetACopiar(const Value: integer);
begin
  if FEstado<>tdsCopying then exit;
  PBCopia.Max := Value;
end;

procedure TFormDBCrtProgress.FormShow(Sender: TObject);
begin
  FEstado := tdsDone;
end;

procedure TFormDBCrtProgress.DBDefProgress(Sender: TDatabaseDef;
  Table: TTableDef; Index: Integer; State: TTableDefState);
begin
  if not Visible then Show;
  Tabla := UpperCase(Table.DatabaseName)+': '+Table.TableName;
  Estado := State;
  ACopiar := Table.RecordsToProcess;
  Copiado := Table.ProcessedRecords;
end;

procedure TFormDBCrtProgress.SetDatabaseDef(const Value: TDatabaseDef);
begin
  FDatabaseDef := Value;
  FDatabaseDef.OnProgress := DBDefProgress;
  Tablas := FDatabaseDef.Definitions.Count;
end;

end.
